package org.shop.pro.config.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.shop.pro.entity.Address;
import org.shop.pro.entity.Shop;
import org.shop.pro.entity.Shopgoods;
import org.shop.pro.entity.Shoptype;
import org.shop.pro.entity.Types;
import org.shop.pro.entity.Vip;
import org.shop.pro.service.AddressService;
import org.shop.pro.service.ShopService;
import org.shop.pro.service.ShoptypeService;
import org.shop.pro.service.TypesService;
import org.shop.pro.service.VipService;
import org.shop.pro.service.shopgoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class UserController {
	@Autowired
	VipService vip;
	@Autowired
	ShoptypeService shoptype;
	@Autowired
	shopgoodsService shopgoods;
	@Autowired
	TypesService types;
	@Autowired
	ShopService shop;
	@Autowired
	AddressService address;

	/***
	 * 12356
	 */
	@RequestMapping("/login")
	public String tologin() {
		return "login";

	}

	@RequestMapping("/logins")
	public String login(String username, int password, Model model, HttpSession session) {
		Vip vv = vip.login(username, password);
		if (vv != null) {
			session.setAttribute("name", vv.getUsername());
			session.setAttribute("id", vv.getId());
			return "ui-me";
		}
		return "login";

	}

	@RequestMapping("/toclass")
	public String classs(Model model) {
		List<Shoptype> list = shoptype.selectAll();
		System.out.println(list.get(1).getShoptype());
		model.addAttribute("shoptypelist", list);
		return "class";
	}

	@RequestMapping("/toindex")
	public String toindex() {
		return "index";
	}



	@RequestMapping("/tomi")
	public String tomi() {
		return "ui-me";
	}

	@ResponseBody
	@RequestMapping("/goodslist")
	public List<Types> goodslist(int goodstypeid) {
		List<Types> list = types.selectlist(goodstypeid);
		return list;
	}

	@RequestMapping("/toshoptype")
	public String toshoptype(int goodstypeid, Model model) {
		List<Shop> shoplist = new ArrayList<>();
		List<Shopgoods> list = shopgoods.goodslist(goodstypeid);
		List<Integer> datalist = new ArrayList<>();
		for (int i = 0; i < list.size(); i++) {

			datalist.add(list.get(i).getShopid());

		}
		shoplist = shop.shoplist(datalist);
		model.addAttribute("shop", shoplist);
		model.addAttribute("shopinfo", list);

		return "shoptype";
	}

	@RequestMapping("/tovipinfo")
	public String tovipinfo(int vipid, Model model) {
		Vip vipinfo = vip.selectByPrimaryKey(vipid);
		model.addAttribute("vipinfo", vipinfo);
		return "vipinfo";
	}

	@RequestMapping("/toaddressinfo")
	public String toaddressinfo(int vipid, Model model) {

		List<Address> list = address.selectByPrimaryKey(vipid);
		model.addAttribute("addressinfo", list);
		model.addAttribute("vipid", vipid);
		return "addressinfo";
	}

	@RequestMapping("/toaddaddress")
	public String toaddaddress(int vipid, Model model, String upd) {

		model.addAttribute("vipid", vipid);
		model.addAttribute("upd", upd);
		return "addaddress";
	}

	@RequestMapping("/goaddaddress")
	public String goaddaddress(int id, Model model, String upd, int vipid) {
		Address addinfo = address.selectlist(id);
		model.addAttribute("addinfo", addinfo);
		model.addAttribute("upd", upd);
		model.addAttribute("vipid", vipid);
		return "addaddress";
	}

	@RequestMapping("/isaddaddress")
	public String addaddress(Address add, String upd,Model model) {
		if (upd == "add"||upd.equals("add")) {
			int ad = address.insert(add);
			if (ad != 0 && add.getAddressdetailed() != "") {
				return "redirect:toaddressinfo?vipid=" + add.getVipid();
			} else {
				return "toaddaddress?vipid=" + add.getVipid();
			}

		} else if (upd == "upd"||upd.equals("upd")) {
			int up = address.updateByPrimaryKey(add);
			if (up != 0) {
				return "redirect:toaddressinfo?vipid=" + add.getVipid();
			} else {
				return "goaddaddress?vipid=" + add.getVipid();
			}
		}
		Address addinfo = address.selectlist(add.getId());
		model.addAttribute("addinfo", addinfo);
		model.addAttribute("upd", upd);
		model.addAttribute("vipid", add.getVipid());
		return "addaddress";
	}
	@ResponseBody
	@RequestMapping("/deladdress")
	public int deladdress(int id) {
		int addressid=address.deleteByPrimaryKey(id);
		return addressid;
	}
}
